package com.slibra.web.controller.common;

import com.slibra.common.config.QiNiuYunProperties;
import com.slibra.common.core.domain.AjaxResult;
import com.slibra.framework.web.service.QiNiuYunServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/qiniuyun")
public class UploadController {

    @Autowired
    QiNiuYunServiceImpl qiNiuYunService;

    @Autowired
    private QiNiuYunProperties qiNiuYunProperties;

    @GetMapping("/test")
    public String test(){
        return "success";
    }


    @PostMapping(value = "/upLoadImage")
    private AjaxResult upLoadImage(@RequestParam("file") MultipartFile file) {
        AjaxResult ajax = AjaxResult.success();
        if (!file.isEmpty()) {
            String url = qiNiuYunService.uploadQNImg(file);
            log.info("七牛云返回的图片链接:{}", url);
            ajax.put("url", url);
            return ajax;
        }
        return ajax;
    }

    /**
     * 内服方法调用
     * @param fileKey
     * @return
     */
    @Deprecated
    @GetMapping(value = "/getPrivateFile/{fileKey}")
    private AjaxResult getPrivateFile(@PathVariable String fileKey) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("url", qiNiuYunService.getPrivateFile(fileKey));
        return ajax;
    }

    /*
     * @Param
     * @return java.lang.String
     **/
    @DeleteMapping("/remove")
    public AjaxResult removeFile(@RequestParam String fileKey){
        qiNiuYunService.removeFile(qiNiuYunProperties.getBucketName(),fileKey);
        return AjaxResult.success("删除成功");
    }

}

